<link rel="stylesheet" href="/static/editor.md/css/editormd.min.css" />
<div class="layui-layout layui-layout-admin" style="padding-left: 40px;margin-top: 20px;">
    <form class="layui-form" action="" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">文档名称</label>
            <div class="layui-input-block " style="width: 400px">
                <input type="text" name="api_public_name" id="api_public_name" lay-verify="required" autocomplete="off" placeholder="比如：调用说明" class="layui-input" value="">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block " style="width: 200px">
                <input type="text" name="sort" id="sort" lay-verify="required" autocomplete="off" placeholder="数字" class="layui-input" value="99">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">选择模板</label>
           
            <div class="layui-input-inline">
                <select name="source_id" lay-filter="templates" lay-verify="required"  lay-search="" style="width: 100%">
                    <option value="0">选择内容模板</option>
                    {{range $index,$elem := .templates}}
                    <option value="{{$elem.Id}}"> {{$elem.TemplateName}}</option>
                    {{end}}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">文档内容</label>
            <div class="layui-input-inline" id="api-editormd" style="border: 1px solid #e4e4e4">
<textarea name="detail" id="detail" style="display:none;">
#### 可以选择模板，亲

</textarea>
            </div>
        </div>

        
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="sub">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script>
var templates = {{.templates}}
var apiEditor;

layui.use(['form','element','table','layer','jquery'],function(){
    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
    var $ = layui.jquery;
    var table = layui.table;

    form.on('submit(sub)', function(data){
        var form_data = data.field;
        $.post('{{urlfor "ApiPublicController.AjaxSave"}}', form_data, function (out) {
            if (out.status == 0) {
                layer.msg("操作成功",{icon: 1},function () {
                    window.location.href="/apipublic/list";
                })
            } else {
                layer.msg(out.message)
            }
        }, "json");
        return false;
    });

    form.on('select(templates)', function(data){
        layer.confirm('是否切换模板？', {
            btn: ['确定', '取消'] //可以无限个按钮
            ,btn2: function(index, layero){
            layer.close(index);
        }
        }, function(index, layero){
            $.each(templates,function(k,v){
                if (v.Id==data.value) {
                    apiEditor.setMarkdown(v.Detail)
                    form.render();
                }
            });
            layer.close(index);
        });
        
    });
  //但是，如果你的HTML是动态生成的，自动渲染就会失效
  //因此你需要在相应的地方，执行下述方法来手动渲染，跟这类似的还有 element.init();
  form.render();
}); 
</script>

{{template "public/editormd.html" .}}